package com.ross.web.maintenance;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.ross.web.dao.CustomersDAO;
import com.ross.web.dao.UsersDAO;
import com.ross.web.domain.Customers;
import com.ross.web.domain.Users;
import com.ross.web.util.DateUtil;
import com.ross.web.util.SessionLogout;

public class CustomerMaintenanceAction extends ActionSupport  {

	private List<Customers> customerList;
	private int dispMode;
	private Customers customers;

	private String searchSukiNo;
	private SessionLogout sessionLogout = new SessionLogout();
	private HttpServletRequest request = ServletActionContext.getRequest();
	CustomersDAO customersDAO = new CustomersDAO();
	
	public String goToCustomerMaintenance(){
		
		if (sessionLogout.SessionExpired()) {
			addActionMessage("Session already expired");
			return "sessionExpired";
		}
		
		customerList = customersDAO.getAllCustomers();
		
		setDispMode(1);
		return SUCCESS;
	}



	public String searchCustomerAction(){
		
		if (sessionLogout.SessionExpired()) {
			addActionMessage("Session already expired");
			return "sessionExpired";
		}
		
		
		customerList = customersDAO.getCustomerByCardNo(searchSukiNo);
		setDispMode(1);
		return SUCCESS;
		
	}
	
	
	public String goToUpdateCustomerAction(){
		if (sessionLogout.SessionExpired()) {
			addActionMessage("Session already expired");
			return "sessionExpired";
		}
		
		customers = customersDAO.getCustomersById(customers.getCustomer_id());
		
		setDispMode(3);
		return SUCCESS;
	}
	
	public String deleteCustomerAction(){
		if (sessionLogout.SessionExpired()) {
			addActionMessage("Session already expired");
			return "sessionExpired";
		}
		
		try{
			
			customersDAO.delete(customers);
			
			addActionMessage(getText("success.customer.delete"));
		}catch (Exception e) {
			e.printStackTrace();
		}
		
		setDispMode(1);
		customerList = customersDAO.getAllCustomers();
		return SUCCESS;
	}
	
	
	public String goToAddCustomerAction(){
		if (sessionLogout.SessionExpired()) {
			addActionMessage("Session already expired");
			return "sessionExpired";
		}
		
		setDispMode(2);
		return SUCCESS;
	}
	
	
	public String doAddCustomerAction(){
		
		if (sessionLogout.SessionExpired()) {
			addActionMessage("Session already expired");
			return "sessionExpired";
		}
		
		
		//System.out.println(customers.getFirstname());
		
		if(!validateInputs()){
			setDispMode(2);
			return ERROR;
		}else{
			insert();
	}
		
		customerList = customersDAO.getAllCustomers();
		setDispMode(1);
		return SUCCESS;
	}
	
	private void insert(){
		Calendar cal = Calendar.getInstance();
		Date timeDate = cal.getTime();
		try{
			customers.setDate_registered(timeDate);
			customers.setLast_transaction_date(timeDate);
			customers.setAccumulated_price(0);
			customersDAO.insert(customers);
			addActionMessage(getText("success.customer.add"));
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	
	private boolean validateInputs(){
		
		if(customers.getFirstname().trim().length() == 0){
			addActionError(getText("error.customer.firstrequired"));
		}else if(customers.getFirstname().trim().length() >= 50){
			addActionError(getText("error.customer.firstnameexceed"));
		}
		
		
		if(customers.getMiddlename().trim().length() == 0){
			addActionError(getText("error.customer.middlenamerequired"));
		}else if(customers.getMiddlename().trim().length() >= 50){
			addActionError(getText("error.customer.middlenameexceed"));
		}
		
		if(customers.getLastname().trim().length()== 0){
			addActionError(getText("error.customer.lastnamerequired"));
		}else if(customers.getLastname().length() >= 50){
			addActionError(getText("error.customer.lastnameexceed"));
		}
		
		if(customers.getSuki_card_no().trim().length() == 0){
			addActionError(getText("error.customer.sukicardrequired"));
		}else if(customers.getSuki_card_no().trim().length() > 50){
			addActionError(getText("error.customer.sukicardexceeded"));
		}
		
		
		
		if(this.hasActionErrors()){
			return false;
		}else{
			return true;
		}
	}
	
	public String doUpdateCustomerAction(){
		if (sessionLogout.SessionExpired()) {
			addActionMessage("Session already expired");
			return "sessionExpired";
		}
		
		if(!validateInputs()){
			setDispMode(3);
			return ERROR;
		}else{
			update();
		}
		
		customerList = customersDAO.getAllCustomers();
		setDispMode(1);
		return SUCCESS;
	}
	
	private void update(){
		try{
			
			customersDAO.update(customers);
			
			addActionMessage(getText("success.customer.update"));
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	
	public Customers getCustomers() {
		return customers;
	}



	public void setCustomers(Customers customers) {
		this.customers = customers;
	}



	public List<Customers> getCustomerList() {
		return customerList;
	}

	public void setCustomerList(List<Customers> customerList) {
		this.customerList = customerList;
	}

	public int getDispMode() {
		return dispMode;
	}

	public void setDispMode(int dispMode) {
		this.dispMode = dispMode;
	}
	
	
	public String getSearchSukiNo() {
		return searchSukiNo;
	}

	public void setSearchSukiNo(String searchSukiNo) {
		this.searchSukiNo = searchSukiNo;
	}
	
	
}
